package com.cskaoyan.controller.wx;


import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.cart.CartCheckedVo;
import com.cskaoyan.bean.cart.CartCheckoutParam;
import com.cskaoyan.bean.cart.CartCheckoutVo;
import com.cskaoyan.bean.cart.CartIndexVo;

import com.cskaoyan.service.wx.CartService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

/**
 * wx端购物车模块
 */

@RestController
@RequestMapping("wx/cart")
public class CartController {

    @Autowired
    CartService cartService;

    //显示购物车页面

    @GetMapping("index")
    public BaseRespVo index() {
        CartIndexVo cartIndexVo = cartService.index();
        return BaseRespVo.ok(cartIndexVo);
    }

    //选中购物车商品，取消勾选购物车商品
    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody Map map, HttpServletRequest request) {
        CartCheckedVo cartCheckedVo = cartService.checked(map);
        return BaseRespVo.ok(cartCheckedVo);
    }

    //编辑按钮，更改购物车模块商品的数量
    //{"productId":103,"goodsId":1086015,"number":3,"id":22}
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Map map) {
        cartService.update(map);
        return BaseRespVo.ok();
    }


    //加入购物车
    //{"goodsId":1109004,"number":3,"productId":138}
    @PostMapping("add")
    public BaseRespVo add(@RequestBody Map map) {
        Integer number = cartService.add(map);
        if(number == null){
            return BaseRespVo.fail("没有这么多库存了！");
        }
        return BaseRespVo.ok(number);
    }

    //删除功能
    //{"productIds":[297]}
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        CartCheckedVo cartCheckedVo = cartService.delete(map);
        return BaseRespVo.ok(cartCheckedVo);
    }


    //下单功能
    //请求参数cartId=0&addressId=0&couponId=0&userCouponId=0&grouponRulesId=0
    @GetMapping("checkout")
    public BaseRespVo checkout(CartCheckoutParam cartCheckoutParam) {
        CartCheckoutVo cartCheckoutVo = cartService.checkout(cartCheckoutParam);
        return BaseRespVo.ok(cartCheckoutVo);
    }

    //点击商品页显示已加入购物车的商品数量
    @GetMapping("goodscount")
    public BaseRespVo goodscount() {
        int number = cartService.goodscount();
        return BaseRespVo.ok(number);
    }


    //立即购买
    //{"goodsId":1181042,"number":2,"productId":289}
    //响应数据：{"errno":0,"data":50,"errmsg":"成功"}
    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody Map map) {

        int number = cartService.fastadd(map);
        if (number == 0) {
            return  BaseRespVo.fail("当前货品的库存量不足");
        }
        return BaseRespVo.ok(number);
    }
}
